/**
 * 
 */
package com.bolbachchan.blog.controller.common;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bolbachchan.blog.hibernate.domain.UserDetails;
import com.bolbachchan.blog.security.BlogUser;
import com.bolbachchan.blog.service.common.UserDetailsService;
import com.bolbachchan.common.ViewConstants;
import com.bolbachchan.common.ui.MessageUtils;
import com.bolbachchan.common.ui.Output;

/**
 * @author Chaitanya
 * 
 */
public abstract class BaseController<FBBType> implements ViewConstants {

    protected static final String FBB = "fbb";

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    protected MessageUtils messageUtils;

    protected UserDetails getUser() {
	BlogUser user = BlogUser.getInstance();
	LdapUserDetailsImpl userDetails = (LdapUserDetailsImpl) SecurityContextHolder.getContext().getAuthentication()
		.getPrincipal();
	BeanUtils.copyProperties(userDetails, user);

	return userDetailsService.getUserDetailsByUsername(user.getUsername());
    }

    @RequestMapping(method = RequestMethod.POST)
    private @ResponseBody
    Output commonPost(@ModelAttribute("fbb") FBBType fbb, BindingResult result) {
	return post(fbb, result);
    }

    public abstract Output post(FBBType fbb, BindingResult result);
}
